package com.lineage.data.item_etcitem.brave;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillHaste;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StatusHaste extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(StatusHaste.class);
  
  private int _time = 300;
  
  private int _gfxid = 0;
  
  private static final int _int7 = 64;
  
  private static final int _int6 = 32;
  
  private static final int _int5 = 16;
  
  private static final int _int4 = 8;
  
  private static final int _int3 = 4;
  
  private static final int _int2 = 2;
  
  private static final int _int1 = 1;
  
  private boolean _isCrown;
  
  private boolean _isKnight;
  
  private boolean _isElf;
  
  private boolean _isWizard;
  
  private boolean _isDarkelf;
  
  private boolean _isDragonKnight;
  
  private boolean _isIllusionist;
  
  private int dele = 1;
  
  public static ItemExecutor get() {
    return new StatusHaste();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (item == null)
      return; 
    if (pc == null)
      return; 
    if (L1BuffUtil.stopPotion(pc))
      if (check(pc)) {
        if (this._gfxid > 0)
          pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), this._gfxid)); 
        if (pc.getHasteItemEquipped() > 0)
          return; 
        if (this.dele == 1)
          pc.getInventory().removeItem(item, 1L); 
        pc.setDrink(false);
        L1BuffUtil.hasteStart(pc);
        L1BuffUtil.cancelAbsoluteBarrier(pc);
        if (pc.hasSkillEffect(29)) {
          pc.killSkillEffectTimer(29);
          pc.sendPacketsAll((ServerBasePacket)new S_SkillHaste(pc.getId(), 0, 0));
        } else if (pc.hasSkillEffect(76)) {
          pc.killSkillEffectTimer(76);
          pc.sendPacketsAll((ServerBasePacket)new S_SkillHaste(pc.getId(), 0, 0));
        } else if (pc.hasSkillEffect(152)) {
          pc.killSkillEffectTimer(152);
          pc.sendPacketsAll((ServerBasePacket)new S_SkillHaste(pc.getId(), 0, 0));
        } else {
          pc.sendPackets((ServerBasePacket)new S_SkillHaste(pc.getId(), 1, this._time));
          pc.broadcastPacketAll((ServerBasePacket)new S_SkillHaste(pc.getId(), 1, 0));
          pc.setMoveSpeed(1);
          pc.setSkillEffect(1001, this._time * 1000);
        } 
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      }  
  }
  
  private boolean check(L1PcInstance pc) {
    try {
      if (pc.isCrown() && this._isCrown)
        return true; 
      if (pc.isKnight() && this._isKnight)
        return true; 
      if (pc.isElf() && this._isElf)
        return true; 
      if (pc.isWizard() && this._isWizard)
        return true; 
      if (pc.isDarkelf() && this._isDarkelf)
        return true; 
      if (pc.isDragonKnight() && this._isDragonKnight)
        return true; 
      if (pc.isIllusionist() && this._isIllusionist)
        return true; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return false;
  }
  
  private void set_use_type(int use_type) {
    try {
      if (use_type >= 64) {
        use_type -= 64;
        this._isIllusionist = true;
      } 
      if (use_type >= 32) {
        use_type -= 32;
        this._isDragonKnight = true;
      } 
      if (use_type >= 16) {
        use_type -= 16;
        this._isDarkelf = true;
      } 
      if (use_type >= 8) {
        use_type -= 8;
        this._isWizard = true;
      } 
      if (use_type >= 4) {
        use_type -= 4;
        this._isElf = true;
      } 
      if (use_type >= 2) {
        use_type -= 2;
        this._isKnight = true;
      } 
      if (use_type >= 1) {
        use_type--;
        this._isCrown = true;
      } 
      if (use_type > 0)
        _log.error("StatusHaste 可执行职业设定错误:余数大于0"); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._time = Integer.parseInt(set[1]);
      if (this._time <= 0) {
        _log.error("StatusHaste 设置错误:技能效果时间小于等于0! 使用预设300秒");
        this._time = 300;
      } 
    } catch (Exception exception) {}
    try {
      this._gfxid = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
    try {
      int user_type = Integer.parseInt(set[3]);
      set_use_type(user_type);
    } catch (Exception exception) {}
    try {
      this.dele = Integer.parseInt(set[4]);
    } catch (Exception exception) {}
  }
}
